// pages/orderDetail/orderDetail.js
var util = require('../../utils/util.js');

Page({
  /**
   * 页面的初始数据
   */
  data: {
    isCustomer: false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    wx.showLoading({
      mask: true,
      title: '加载中...',
    })
    var id = options.id;
    this.loadData(id);
  },

  loadData: function(id){
    var that = this;
    wx.getStorage({
      key: 'cookie',
      success: function (res) {
        var cookie = res.data;
        var header = util.getCommonHeaders(cookie);
        wx.request({
          url: 'https://bookin.moha.vip/transaction/' + id + "/",
          header: header,
          success: function (res) {
            if (res.statusCode === 200) {
              that.setData({
                order: res.data["data"]
              });
              wx.getStorage({
                key: 'username',
                success: function (r) {
                  that.setData({
                    username: r.data,
                    isCustomer: r.data === res.data["data"].buyer
                  });
                },
              })
            } else {
              wx.showToast({
                icon: "none",
                title: res.data["message"],
                complete: () => {
                  wx.navigateBack({})
                }
              })
            }
          },
          fail: () => {
            wx.navigateBack({
            })
          },
          complete: function () {
            wx.hideLoading();
          }
        })
      },
      fail: () => {
        wx.hideLoading();
      }
    });
  },

  toBookDetail: function () {
    var that = this;
    wx.navigateTo({
      url: '/pages/bookDetail/bookDetail?id=' + that.data.order.book.book_id,
    })
  },

  toContact: function () {
    var that = this;
    wx.navigateTo({
      url: '/pages/chat/interaction/interaction?id=' + that.data.order.buyer,
    })
  },

  toConfirmOrder: function () {
    var that = this;
    var url = util.getRoot() + "/transaction/" + this.data.order.transaction_id + "/";
    this.confirmAlert("确认订单", function () {
      wx.showLoading({
        title: '提交中...',
      })
      wx.getStorage({
        key: 'cookie',
        success: function(res) {
          var cookie = res.data;
          var header = util.getCommonHeaders(cookie);
          wx.request({
            url: url,
            header: header,
            method: "post",
            success: res=>{
              if(res.statusCode === 200){
                wx.showToast({
                  title: '确认成功！',
                });
                that.loadData(that.data.order.transaction_id);
              }else{
                wx.showToast({
                  icon: "none",
                  title: res.data["message"],
                })
              }
            },
            complete: ()=>{
              wx.hideLoading();
            }
          })
        },
        fail: () =>{
          wx.hideLoading();
        }
      })
    })
  },

  toCancelOrder: function () {
    var that = this;
    var url = util.getRoot() + "/transaction/" + this.data.order.transaction_id + "/";
    this.confirmAlert("取消订单", function () {
      wx.showLoading({
        title: '提交中...',
      })
      wx.getStorage({
        key: 'cookie',
        success: function (res) {
          var cookie = res.data;
          var header = util.getCommonHeaders(cookie);
          wx.request({
            url: url,
            header: header,
            method: "delete",
            success: res => {
              if (res.statusCode === 200) {
                wx.showToast({
                  title: '取消成功！',
                })
                that.loadData(that.data.order.transaction_id);
              } else {
                wx.showToast({
                  icon: "none",
                  title: '未知错误',
                })
              }
            },
            complete: () => {
              wx.hideLoading();
            }
          })
        },
        fail: () => {
          wx.hideLoading();
        }
      })
    })
  },

  toFinishOrder: function () {
    var that = this;
    var url = util.getRoot() + "/transaction/" + this.data.order.transaction_id + "/";
    this.confirmAlert("结束订单", function () {
      wx.showLoading({
        title: '提交中...',
      })
      wx.getStorage({
        key: 'cookie',
        success: function (res) {
          var cookie = res.data;
          var header = util.getCommonHeaders(cookie);
          wx.request({
            url: url,
            header: header,
            method: "post",
            success: res => {
              if (res.statusCode === 200) {
                wx.showToast({
                  title: '完成订单！',
                })
                that.loadData(that.data.order.transaction_id);
              } else {
                wx.showToast({
                  icon: "none",
                  title: res.data["message"],
                })
              }
            },
            complete: () => {
              wx.hideLoading();
            }
          })
        },
        fail: () => {
          wx.hideLoading();
        }
      })
    })
  },

  confirmAlert: function (action, func) {
    wx.showModal({
      title: '提示',
      content: '确定要' + action + "吗？请仔细检查，避免财产损失",
      success: (res)=>{
        if(res.cancel) return;
        func();
      }
    })
  }
})